package com.ds.infrastructure.hyperspace.console.controller;

import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbOperator;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.form.PageForm;
import com.ds.infrastructure.hyperspace.console.model.request.operator.ListOperatorForm;
import com.ds.infrastructure.hyperspace.console.model.request.operator.SaveOperatorForm;
import com.ds.infrastructure.hyperspace.console.model.response.operator.GetOperatorVO;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultData;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultList;
import com.ds.infrastructure.hyperspace.console.service.inf.IOperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author: yaozhendong
 * @create: 2019-09-25 17:54
 **/

@RestController
@RequestMapping(value = "/operator", produces = {"application/json;charset=UTF-8"})
@Api(value = "Manager Operator Controller", tags = "操作符管理")
public class OperatorController {
    @Autowired
    IOperatorService iOperatorService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存操作符")
    public JsonResultData<Long> save(@RequestBody @Valid SaveOperatorForm form) {
        TbOperator item = iOperatorService.selectOne(SaveOperatorForm.toEntity4SelectOne(form), null);
        if (item != null) {
            throw new HyperspaceException("保存操作符失败，操作符已经存在，operatorName=" + form.getOperatorName() + ";operatorType=" + form.getOperatorType());
        }
        TbOperator entity = SaveOperatorForm.toEntity(form);
        iOperatorService.save(entity);
        return JsonResultData.build(entity.getId());
    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "操作符列表")
    public JsonResultList<GetOperatorVO> list(@RequestBody @Valid ListOperatorForm form) {
        TbOperator where = ListOperatorForm.toEntity(form);
        TbPageWhere pageWhere = PageForm.toPageWhere(form);
        int dataTotal = iOperatorService.getTotal(where, null);
        if (dataTotal == 0 || pageWhere.getBegin() > dataTotal) {
            return JsonResultList.build(Collections.emptyList(), dataTotal);
        }
        List<TbOperator> tbOperatorList = iOperatorService.getList(where, null, pageWhere);
        List<GetOperatorVO> dataList = new ArrayList<>();
        tbOperatorList.forEach(item -> dataList.add(GetOperatorVO.toVO(item)));
        return JsonResultList.build(dataList, dataTotal);
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "查看操作符")
    public JsonResultData<GetOperatorVO> get(@RequestParam(name = "id") Long id) {
        TbOperator entity = iOperatorService.get(id);
        if (entity == null) {
            throw new HyperspaceException("查看操作符失败，没有找到对应的记录;当前id=" + id);
        }
        GetOperatorVO result = GetOperatorVO.toVO(entity);
        return JsonResultData.build(result);
    }
}
